package com.Glow.reggie.service.impl;

import com.Glow.reggie.common.CustomException;
import com.Glow.reggie.entity.Category;
import com.Glow.reggie.entity.Dish;
import com.Glow.reggie.entity.Setmeal;
import com.Glow.reggie.mapper.CategoryMapper;
import com.Glow.reggie.service.CategoryService;
import com.Glow.reggie.service.DishService;
import com.Glow.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Glow
 * @Date 2022-07-30 14:33:45
 * @Description
 * @Version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 根据Id删除分类，删除之前判断是否有菜品或套餐相关联
     * @param id
     */
    @Override
    public void remove(Long id) {
         //查看是否关联菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(dishLambdaQueryWrapper);
        if(count > 0)
        {
            throw new CustomException("当前分类已关联菜品，无法删除");
        }
        //查看是否关联套餐
        LambdaQueryWrapper<Setmeal>setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int setMealCount = setmealService.count(setmealLambdaQueryWrapper);
        if(setMealCount > 0)
        {
            throw new CustomException("当前分类已关联套餐，无法删除");
        }
        //如果都没关联，则可以删除辣
        super.removeById(id);
    }
}
